import { AppDataSource } from './data.source'
import { User } from './entity/User'

// 首先初始化
AppDataSource.initialize().then(async () => {
    const queryBuilder = AppDataSource.manager.createQueryBuilder()
    // 插入数据
    await queryBuilder.insert().into(User).values([
        {
            firstName: 'firstName01',
            lastName: 'lastName01',
            age: 18,
            email: (new Date().getTime() + 1) + '@qq.com',
            isActive: true,
        },
        {
            firstName: 'firstName02',
            lastName: 'lastName02',
            age: 28,
            email: (new Date().getTime() + 2) + '@qq.com',
            isActive: true,
        }
    ]).execute();

    // 查询
    const users = await queryBuilder.select("user").from(User, 'user').where("user.id>:id", { id: 1 }).getMany();
    console.log(users);

    // 更新 email为2646468888@qq.com 的对象中的age为30
    await queryBuilder.update(User).set({ age: 30 }).where("user.email=:email", { email: "2646468888@qq.com" }).execute();

    // 删除 id=5
    await queryBuilder.delete().from(User).where("id=:id",{id:5}).execute()
})